package oct2013;

public class SortColors {
	public void sortColors(int[] A) {
		int n = A.length;
		int i = 0, j = n - 1, k = 0;
		while (k <= j) {
			if (A[k] == 0) {
				swap(A, i++, k++);
			} else if (A[k] == 2) {
				swap(A, k, j--);
			} else {
				k++;
			}
		}
	}

	void swap(int[] A, int i, int j) {
		if (i != j) {
			A[i] ^= A[j];
			A[j] ^= A[i];
			A[i] ^= A[j];
		}
	}
}
